package com.jie.jie.PermissionDispatcher;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.jaydenxiao.common.commonutils.LogUtils;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.jie.jie.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class DuoActivity extends AppCompatActivity {

    @Bind(R.id.duo_btn)
    Button duoBtn;
    @Bind(R.id.activity_duo)
    RelativeLayout activityDuo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duo);
        ButterKnife.bind(this);
    }

    /**
     * 多个权限时,自己的弹框太会调用@onpermissionDenied方法,系统弹框不会调用
     */


    @OnClick(R.id.duo_btn)
    public void onClick() {
        DuoActivityPermissionsDispatcher.readSMSWithCheck(this);
    }

    @NeedsPermission({Manifest.permission.READ_SMS, Manifest.permission.CAMERA})
    void readSMS() {
        LogUtils.loge("请求多个权限");
        ToastUitl.showShort("请求多个权限");
    }

    @OnPermissionDenied({Manifest.permission.READ_SMS, Manifest.permission.CAMERA})
    void jj() {
        LogUtils.loge("拒绝权限");
        ToastUitl.showShort("拒绝权限");
    }

    @OnNeverAskAgain({Manifest.permission.READ_SMS, Manifest.permission.CAMERA})
    void neverAsk() {
        LogUtils.loge("不在提示");
        ToastUitl.showShort("拒绝权限,不在提示");
    }

    @OnShowRationale({Manifest.permission.READ_SMS, Manifest.permission.CAMERA})
    void show(PermissionRequest request) {
        LogUtils.loge("需要授予权限 dailog");
        showDialog("需要授予权限", request);
    }

    private void showDialog(String msg, final PermissionRequest request) {
        new AlertDialog.Builder(this)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.proceed();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(@NonNull DialogInterface dialog, int which) {
                        request.cancel();
                    }
                })
                .setCancelable(false)
                .setMessage(msg)
                .show();
    }

    
}
